
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Encyclopedia;

public class GenericEncyclopediaDAO extends AutoKeys implements org.jetnuke.dao.EncyclopediaDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.EncyclopediaModel#retrieveByPK(int)
	 */
	public Encyclopedia retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("EncyclopediaModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Encyclopedia  s = new Encyclopedia ();
		
		if (rs.next()) {
			fillEncyFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillEncyFromResultSet(Encyclopedia s, ResultSet rs)throws Exception {
		s.setACTIVE(rs.getInt("ACTIVE"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setEID(rs.getInt("EID"));
		s.setELANGUAGE(rs.getString("ELANGUAGE"));
		s.setTITLE(rs.getString("TITLE"));
		
	}
	
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Encyclopedia u = new Encyclopedia();
			fillEncyFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.EncyclopediaModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p;
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("EncyclopediaModel.selectAll"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.EncyclopediaModel#selectAllActive()
	 */
	public List selectAllActive() throws Exception {
		PreparedStatement p;
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("EncyclopediaModel.selectAllActive"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.EncyclopediaModel#addNew(org.jetnuke.entities.Encyclopedia)
	 */
	public int addNew(Encyclopedia ency) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("EncyclopediaModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("EncyclopediaModel.lastGeneratedEncyID"));
		initEncy(ency, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}

	/**
	 * @param ency
	 * @param p
	 */
	private void initEncy(Encyclopedia ency, PreparedStatement p)throws Exception {
		p.setInt(1,ency.getACTIVE());
		p.setString(2,ency.getDESCRIPTION());
		p.setString(3,ency.getELANGUAGE());
		p.setString(4,ency.getTITLE());
		
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.EncyclopediaModel#update(org.jetnuke.entities.Encyclopedia)
	 */
	public void update(Encyclopedia ency) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("EncyclopediaModel.update");
		p.setInt(1,ency.getACTIVE());
		p.setString(2,ency.getDESCRIPTION());
		p.setString(3,ency.getELANGUAGE());
		p.setString(4,ency.getTITLE());
		p.setInt(5,ency.getEID());
		p.executeUpdate();
		p.close();
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.EncyclopediaModel#selectAllInactive()
	 */
	public List selectAllInactive() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("EncyclopediaModel.selectAllInactive"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

}
